package org.raymond.icloud.modules.admin.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.assertj.core.util.Lists;
import org.raymond.icloud.apis.admin.entity.TOrgRoleRel;
import org.raymond.icloud.apis.admin.entity.TSysRole;
import org.raymond.icloud.apis.admin.entity.TUserRoleRel;
import org.raymond.icloud.apis.admin.entity.enums.StateEnum;
import org.raymond.icloud.modules.admin.mapper.TSysRoleMapper;
import org.raymond.icloud.modules.admin.service.ITOrgRoleRelService;
import org.raymond.icloud.modules.admin.service.ITSysRoleService;
import org.raymond.icloud.modules.admin.service.ITUserRoleRelService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * @author raymond
 * @version V1.0
 * @Description: TODO
 */
@Slf4j
@Service
public class TSysRoleServiceImpl extends ServiceImpl<TSysRoleMapper, TSysRole> implements ITSysRoleService {
    @Autowired
    private ITUserRoleRelService itUserRoleRelService;

    @Override
    public List<TSysRole> getsChildRole(Set<Long> roleIdSet) {
        if(CollectionUtil.isEmpty(roleIdSet)){
            return null;
        }
        LambdaQueryWrapper<TSysRole> listRoleWrapper = new LambdaQueryWrapper<>();
        listRoleWrapper.in(TSysRole::getParentId, roleIdSet);
        listRoleWrapper.eq(TSysRole::getState, StateEnum.NORMAL);
        List<TSysRole> roleList = list(listRoleWrapper);
        Set<Long> childRoleIdSet = roleList.stream().mapToLong(TSysRole::getId).boxed().collect(Collectors.toSet());
        List<TSysRole> childRoleList = getsChildRole(childRoleIdSet);
        if(CollectionUtil.isNotEmpty(childRoleList)){
            roleList.addAll(childRoleList);
        }
        return roleList;
    }

    @Override
    public List<TSysRole> getsAssociatedRoleByUserId(Long userId) {
        if(userId==null) {
            return null;
        }

        LambdaQueryWrapper<TUserRoleRel> listRelWrapper = new LambdaQueryWrapper<>();
        listRelWrapper.select(TUserRoleRel::getUserId, TUserRoleRel::getRoleId);
        listRelWrapper.in(TUserRoleRel::getUserId, userId);
        List<TUserRoleRel> userRoleRels = itUserRoleRelService.list(listRelWrapper);
        if(CollectionUtil.isEmpty(userRoleRels)) {
            return null;
        }
        Set<Long> directRoleIdSet = userRoleRels.stream().mapToLong(TUserRoleRel::getRoleId).boxed().collect(Collectors.toSet());
        LambdaQueryWrapper<TSysRole> listDirectRoleWrapper = new LambdaQueryWrapper<>();
        listDirectRoleWrapper.in(TSysRole::getId, directRoleIdSet);
        listDirectRoleWrapper.eq(TSysRole::getState, StateEnum.NORMAL);
        List<TSysRole> directRoleList = list(listDirectRoleWrapper);
        if(CollectionUtil.isEmpty(directRoleList)) {
            return null;
        }
        directRoleIdSet = directRoleList.stream().mapToLong(TSysRole::getId).boxed().collect(Collectors.toSet());
        List<TSysRole> childRoleList = getsChildRole(directRoleIdSet);
        List<TSysRole> allRelRoleList = Lists.newArrayList();
        allRelRoleList.addAll(directRoleList);
        allRelRoleList.addAll(childRoleList);
        return allRelRoleList;
    }
}
